In this part of the course, we will cover the following concepts:
In this module, we will explore basic control flow structures in Python including conditionals and loops
Before beginning, let’s look at some examples of conditional and looping situations you may experience in your everyday life
Which of these situations represents conditional logic?
Situation A: You drive to work every day in your car.
Situation B: If you are at least 16 years old, you can drive a car in the U.S. Otherwise, you are not allowed to drive a car in the U.S.
Which of the situations below represents a loop?
Situation A: Your alarm clock is set for every morning at 7:00.
Situation B: If it’s Saturday or Sunday, your alarm clock is off. All other days, your alarm clock is set for 6:30 in the morning.
Which of the situations below represents conditional logic?
Situation A: If you’re really hungry, you eat eggs, bacon, and toast for breakfast. Otherwise, you eat a bowl of cereal.
Situation B: You eat a bowl of cereal every day for breakfast.
Use the microphone or chat to share your ideas about the following questions:
| Objective | Complete |
|---|---|
| Discuss control flow structures and the practice of writing modular code | |
| Use conditional statements such as if / else |
We will discuss the following control flow structures:
if-else statements)for and while structures)def structure)Control flow structures allow us to:
| Objective | Complete |
|---|---|
| Discuss control flow structures and the practice of writing modular code |
✔ |
| Use conditional statements such as if / else |
Conditional statements allow our program to decide whether or not to run certain sections of code, based on some condition
In Python, we use if and else blocks to build conditional statements
The condition itself follows the if statement
The alternative route is given by the else block
| Operator | Example |
|---|---|
| Greater than | x > y |
| Less than | x < y |
| Equal to | x == y |
| Not equal to | x != y |
| Greater than or equal to | x >= y |
| Less than or equal to | x <= y |
and and or to check for a combination of conditions# Check if 2 conditions are true
# for the expression to return `True`!
x = 8
print(x > 5 and x < 10)True
add to add as many statements as we want, but when Python checks the conditions, it will only return True if all of the conditions are met# Every single condition much be true for this expression to return `True`!
print(x > 5 and x > 1 and abs(x) == 7 and x < 10)False
abs(x) is equal to 8 not 7, the entire expression returns False2 + 2 == 4) can also be assigned to variablesif-else blocksTrue
False
False
compound_condition1 = (5 + 10 > 10 + 5) and ("this string" == "this string")
print(compound_condition1)False
compound_condition2 = (5 + 10 >= 10 + 5) and ("this string" == "this string")
print(compound_condition2)True
# Here, it's helpful to look at each condition individually.
# If one of them is true, then the whole statement is true.
compound_condition3 = (compound_condition1 and compound_condition2) or (100/2 > 100 % 2)
print(compound_condition3)True
if-else statement with a compound condition written directly within itif (5 + 10 > 10 + 5) and ("this string" == "this string"):
print('Compound condition 1 is true, do something!')
else:
print('Compound condition 1 is false, do something else!')Compound condition 1 is false, do something else!
if-else statement with the compound condition saved to a variable beforehandcompound_condition1 = (5 + 10 > 10 + 5) and ("this string" == "this string")
if compound_condition1:
print('Compound condition 1 is true, do something!')
else:
print('Compound condition 1 is false, do something else!')Compound condition 1 is false, do something else!
if-else formif block will do the trick!Ok, I guess I have to do something after all!
if yet_another_condition:
print("This means the `compound_condition3` is true, otherwise you will get nothing!")print statement is not triggered because yet_another_condition is elifelif treeprice = 37000
if price > 40000:
print("That's too expensive!")
elif price > 34000:
print('A little pricey but maybe worth it...')
elif price > 26000:
print("This seems like a fair price for the quality")
elif price > 22000:
print("What a good deal! I'll get it")
else:
print("Hmmm this is pretty cheap, maybe there's a problem with it.")A little pricey but maybe worth it...
if condition:
if another_condition:
print("You got to a nested statement!")
else:
print("You still got to the nested statement!")
else:
print("No luck printing a nested statement!")You still got to the nested statement!
if and elif blocksprice = 37000
account_balance = 45000
if price > 38000:
action = "Leave the dealership immediately, this is a rip off!"
account_balance = account_balance - price
elif price > 22000 and price <= 38000 :
action = "Take the car and go celebrate, you can afford it!"
account_balance = account_balance - price
else:
action = "Leave the dealership immediately, this is a scam!"
account_balance = account_balance - price
print(action)Take the car and go celebrate, you can afford it!
Current account balance: 8000
price and print resultsprice = 41000
account_balance = 45000
if price > 38000:
action = "Leave the dealership immediately, this is a rip off!"
account_balance = account_balance - price
elif price > 22000 and price <= 38000 :
action = "Take the car and go celebrate, you can afford it!"
account_balance = account_balance - price
else:
action = "Leave the dealership immediately, this is a scam!"
account_balance = account_balance - price
print(action)Leave the dealership immediately, this is a rip off!
Current account balance: 4000
Any action can be housed within a conditional block
Actions we might take include:
| Objective | Complete |
|---|---|
| Discuss control flow structures and the practice of writing modular code |
✔ |
| Use conditional statements such as if / else |
✔ |
You are now ready to try Tasks 1-4 in the Exercise for this topic